package lab2_3;

import java.util.Scanner;
import java.util.Arrays;

public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String[] ids=new String[n];
		for(int i=0;i<n;i++)
		{
			ids[i]=sc.next();
		}
		String[] getids=new String[n];
		for(int i=0;i<n;i++)
		{
			getids[i]=getBirthDate(ids[i]);
		}		
		while(sc.hasNextLine())
		{
			String choice="";
			choice=sc.next();
			if(choice.equals("sort1"))
			{
				
				Arrays.sort(getids);
				for(int i=0;i<n;i++)
				{
						System.out.println(getids[i]);
				}
			}
			else if(choice.equals("sort2"))
			{			
				for(int m=0;m<n;m++)
				{
					for(int t=m;t<n;t++)
					{
						if(getids[m].compareTo(getids[t])>0)
						{
							String s=getids[m];
							getids[m]=getids[t];
							getids[t]=s;
							String ss=ids[m];
							ids[m]=ids[t];
							ids[t]=ss;
						}
					}
				}
				for(int i=0;i<n;i++)
				{
						System.out.println(ids[i]);
				}
			}
			else
				System.out.println("exit");
		}

     
 }
	private static String getBirthDate(String id) {
		// TODO Auto-generated method stub
		String year=id.substring(6,10);
		String month=id.substring(10,12);
		String day=id.substring(12,14);
		return year+"-"+month+"-"+day;
	}
}
